package com.conveyal.datatools.editor.models;

import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.UUID;

@MappedSuperclass
public class Model implements Cloneable, Serializable {
    private static final long serialVersionUID = -2490147875977011741L;

    /** not final for purposes of deserialization, but don't change it once data has been persisted */
    public String id;

    /** Set the ID, unless it is null, in which case keep the generated ID */
    public void setId (String id) {
        if (id != null)
            this.id = id;
    }

    /** Create a new model with an autogenerated ID */
    public Model () {
        generateId();
    }

    /**
     * Create a new model with the specified ID.
     */
    public Model (String id) {
        this.id = id;
    }

    public void generateId () {
        id = UUID.randomUUID().toString();
    }
}
